import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken, setRefreshToken } from '@/utils/auth'
import { areaInfo } from '@/api/safeGeneral'
const user = {
    state: {
        token: getToken(),
        user: {},
        areaList: [],
        pageType: sessionStorage.getItem("thirdPageType") ? sessionStorage.getItem("thirdPageType") : '',
        thirdPageUser: JSON.parse(sessionStorage.getItem("thirdPageUser")) ? JSON.parse(sessionStorage.getItem("thirdPageUser")) : {},
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_USER: (state, user) => {
            state.user = user
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_LOAD_MENUS: (state, loadMenus) => {
            state.loadMenus = loadMenus
        },
        SET_AREA_LIST: (state, areaList) => {
            state.areaList = areaList
        },
        SET_PAGE_TYPE: (state, pageType) => {
            state.pageType = pageType;
            sessionStorage.setItem('thirdPageType', pageType);
        },
        SET_THIRD_PAGEUSER: (state, pageUser) => {
            state.thirdPageUser = pageUser;
            sessionStorage.setItem('thirdPageUser', JSON.stringify(pageUser));
        },

    },

    actions: {
        // 登录
        Login({ commit }, userInfo) {
            // debugger
            const rememberMe = userInfo.rememberMe
            return new Promise((resolve, reject) => {
                login(userInfo.username, userInfo.password).then(res => {
                    console.log(res);
                    if (res.isSuccess == true) {
                        setToken(res.data, rememberMe)
                        commit('SET_TOKEN', res.data)
                        commit('SET_USER', res.user)
                    } else {
                        setToken(res.data, rememberMe)
                    }
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 获取用户信息
        GetInfo({ commit }) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    commit('SET_USER', res.content)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        getAreaList({ commit }) {
            return new Promise((resolve, reject) => {
                areaInfo().then(res => {
                    commit('SET_AREA_LIST', res.content)
                    resolve(res.content)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        LogOut({ commit }) {
            logOut(commit)
        },
        setPageType: ({ commit }, val) => {
            commit("SET_PAGE_TYPE", val);
        },
        setThirdPageUser: ({ commit }, val) => {
            commit("SET_THIRD_PAGEUSER", val);
        },

    }
}

export const logOut = commit => {
    commit('SET_TOKEN', '')
    commit('SET_ROLES', [])
    removeToken()
}

export const setUserInfo = (res, commit) => {
    // 如果没有任何权限，则赋予一个默认的权限，避免请求死循环
    if (res.roles.length === 0) {
        commit('SET_ROLES', ['ROLE_SYSTEM_DEFAULT'])
    } else {
        commit('SET_ROLES', res.roles)
    }
    commit('SET_USER', res.user)
}

export default user
